//
//  ChangeTelViewController.m
//  Kjs-iOS
//
//  Created by cfq on 16/6/9.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "ChangeTelViewController.h"
#import "KJSRequest.h"
#import "UIColor+KjsColor.h"
#import "AppDelegate.h"
#import "CommonUtil.h"
#define CodeTimeInterval 180


@interface ChangeTelViewController ()
@property(nonatomic,weak) IBOutlet UIButton *getCodeBtn;
@property(nonatomic,weak) IBOutlet UIButton *resetTelBtn;
@property (nonatomic,weak) IBOutlet UITextField *phoneNumTF;
@property (nonatomic,weak) IBOutlet UITextField *codeTF;

@property (nonatomic,assign) int secondsCountDown;//倒计时总时长
@property (nonatomic,strong) NSTimer *countDownTimer;
@end

@implementation ChangeTelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"手机绑定"];
    self.resetTelBtn.layer.cornerRadius = 10;
    self.getCodeBtn.layer.cornerRadius = 15;
    self.secondsCountDown = CodeTimeInterval;
    
    [self.resetTelBtn addTarget:self action:@selector(bindNewPhone) forControlEvents:UIControlEventTouchUpInside];
    [self.getCodeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initConfigeration];
}
#pragma mark - configeration
-(void)initConfigeration
{
    self.phoneNumTF.text = @"";
    self.codeTF.text = @"";
    self.resetTelBtn.enabled = NO;
    [self.resetTelBtn setBackgroundColor:[UIColor lightGrayColor]];
}
#pragma mark - custom method
-(void)sendMessage
{
    self.resetTelBtn.enabled = YES;
    [self.resetTelBtn setBackgroundColor:[UIColor kjsMainColor]];
    if ([self.phoneNumTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneNumTF.text]) {
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else{
        [KJSRequest BindTelSendMessageWithPhoneNumber:self.phoneNumTF.text ret:^(BasicResult *model) {
            if ([model.messageresult isEqualToString:@"0"]) {
                [GUATool showDialog:@"验证码已发送" message:nil waitingTime:1.5];
                self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            }
        }];
    }
}
-(void)bindNewPhone
{
    
    if ([self.phoneNumTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneNumTF.text]) {
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else{
        [KJSRequest BindTelWithUserLoginId:self.phoneNumTF.text verificationCode:self.codeTF.text ret:^(BasicResult *model) {
            if ([model.returnMsg isEqualToString:@"success"]){
                UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"手机号码更换成功，请重新登录" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertV show];
            }
        }];
    }
}
-(void)timeFireMethod{
    
    self.secondsCountDown--;
    self.getCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%d秒",self.secondsCountDown];
    [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%d秒",self.secondsCountDown] forState:UIControlStateNormal];
    self.getCodeBtn.enabled = NO;
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.getCodeBtn.enabled = YES;
        self.secondsCountDown = CodeTimeInterval;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0){
        [self bindSuccessReturn];
    }
}
-(void)bindSuccessReturn{
    if (self.bindSuccess) {
        self.bindSuccess();
    }
    [self dismiss:YES];
}
@end
